package org;

import org.entities.LevelObject;
import org.entities.Player;
import org.newdawn.slick.Color;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.transition.FadeInTransition;

public class Transition extends BasicGameState {


    public static final int ID = 5;

    public int getID() {
        return ID;
    }

    public void init(GameContainer container, StateBasedGame game) {
    }

    public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {

        // =====================
        // door code
        // =====================

        if (Progress.gameOver){
            GameState.bmap = null;
            GameState.bmap = new BlockMap("data/level/level0001.tmx");
            Progress.gameOver=false;
        }
        if (!Progress.gameOver){
            if (Player.appearLeftDown&&Player.doorRU) {
                System.out.printf("read doorRU maprrr=[level%s.tmx]\n", BlockMap.rightUpNu);
                GameState.bmap = null;
                GameState.bmap = new BlockMap("data/level/level" + BlockMap.rightUpNu + ".tmx");
            } 
            if (Player.appearLeftUp&&Player.doorRU) {
                System.out.printf("read doorRU map=[level%s.tmx]\n", BlockMap.rightUpNu);
                GameState.bmap = null;
                GameState.bmap = new BlockMap("data/level/level" + BlockMap.rightUpNu + ".tmx");
            } 
            if (Player.appearLeftDown&&Player.doorRD) {
                System.out.printf("read doorRD map=[level%s.tmx]\n", BlockMap.rightNu);
                GameState.bmap = null;
                GameState.bmap = new BlockMap("data/level/level" + BlockMap.rightNu + ".tmx");
            } 
            if (Player.appearLeftUp&&Player.doorRD) {
                System.out.printf("read doorRD map=[level%s.tmx]\n", BlockMap.rightNu);
                GameState.bmap = null;
                GameState.bmap = new BlockMap("data/level/level" + BlockMap.rightNu + ".tmx");
            }
            if (Player.appearRightUp&&Player.doorLU) {
                //System.out.printf("read doorLU map=[level%s.tmx]\n", BlockMap.leftUpNu);
                GameState.bmap = null;
                GameState.bmap = new BlockMap("data/level/level" + BlockMap.leftUpNu + ".tmx");
            }
            if (Player.appearRightDown&&Player.doorLU) {
                //System.out.printf("read doorLU map=[level%s.tmx]\n", BlockMap.leftUpNu);
                GameState.bmap = null;
                GameState.bmap = new BlockMap("data/level/level" + BlockMap.leftUpNu + ".tmx");
            }
            if (Player.appearRightUp&&Player.doorLD) {
                //System.out.printf("read doorLD mapttt=[level%s.tmx]\n", BlockMap.leftNu);
                GameState.bmap = null;
                GameState.bmap = new BlockMap("data/level/level" + BlockMap.leftNu + ".tmx");
            }
            if (Player.appearRightDown&&Player.doorLD) {
                //System.out.printf("read doorLD map=[level%s.tmx]\n", BlockMap.leftNu);
                GameState.bmap = null;
                GameState.bmap = new BlockMap("data/level/level" + BlockMap.leftNu + ".tmx");
            }
        }


        // =====================
        // go back to gamestate
        // =====================
        game.enterState(GameState.ID, new FadeInTransition(Color.black), null);
        GameState.restartGame = false;
    }


    public void render(GameContainer container, StateBasedGame game, Graphics g) {

        // ============================
        // render something for fadeOut 
        // ============================
        if (!LevelObject.debugMode)
        g.scale(2f, 2f);
        // scenery
        BlockMap.tmap.render(
                (int) GameState.mapX % 16,
                (int) GameState.mapY % 16,
                (int) (-GameState.mapX / 16),
                (int) (-GameState.mapY / 16),
                (GameState.viewWidth / 16) + 2,
                (GameState.viewHeight / 16) + 2,
                1, false);

        // background tiles
        BlockMap.tmap.render(
                (int) GameState.mapX % 16,
                (int) GameState.mapY % 16,
                (int) (-GameState.mapX / 16),
                (int) (-GameState.mapY / 16),
                (GameState.viewWidth / 16) + 2,
                (GameState.viewHeight / 16) + 2,
                2, false);

        //foreground tiles
        BlockMap.tmap.render(
                (int) GameState.mapX % 16,
                (int) GameState.mapY % 16,
                (int) (-GameState.mapX / 16),
                (int) (-GameState.mapY / 16),
                (GameState.viewWidth / 16) + 2,
                (GameState.viewHeight / 16) + 2,
                3, false);

    }
}
